package com.hjm.ui.comment;

import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjm.ui.R;
import com.hjm.ui.comment.adapter.CommentAdapter;
import com.hjm.ui.comment.entity.Comment;

import java.util.ArrayList;
import java.util.List;

public class CommentFragment extends Fragment {
    private List<Comment> commentList;
    private RecyclerView recyclerView;
    private CommentAdapter commentAdapter;
    private ImageButton addComment;
    private View.OnClickListener onClickListener;
    private boolean isAddButtonNeedToHide = false;

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
        if (addComment != null)
            addComment.setOnClickListener(onClickListener);
    }

    public static CommentFragment newInstance(List<Comment> commentList) {
        CommentFragment commentFragment = new CommentFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("commentList", (ArrayList<? extends Parcelable>) commentList);
        commentFragment.setArguments(bundle);
        return commentFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            commentList = getArguments().getParcelableArrayList("commentList");
            Log.d("评论片段", "onCreate: 数据大小 "+commentList.size());
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_comment, container, false);
        recyclerView = view.findViewById(R.id.comment_recycler_view);

        // 设置RecyclerView的布局管理器，垂直1列，中间隔开20dp
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(layoutManager);
        // 设置分割线
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        // 设置垂直间隔
        // 设置RecyclerView的适配器
        commentAdapter = new CommentAdapter(commentList,getContext());
        recyclerView.setAdapter(commentAdapter);


        addComment = view.findViewById(R.id.comment_add_button);
        if (onClickListener != null)
            addComment.setOnClickListener(onClickListener);
        if (isAddButtonNeedToHide)
            addComment.setVisibility(View.GONE);
        return view;
    }
    public void hideAddButton(){
        this.isAddButtonNeedToHide = true;
        if (addComment != null){
            addComment.setVisibility(View.GONE);
        }
    }
}
